package com.jiangjiang.eshop.commodity.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.commodity.convert.PropertyConvert;
import com.jiangjiang.eshop.commodity.dao.PropertyMapper;
import com.jiangjiang.eshop.commodity.dto.PropertyDTO;
import com.jiangjiang.eshop.commodity.dto.PropertyPageQueryDTO;
import com.jiangjiang.eshop.commodity.dto.PropertySaveDTO;
import com.jiangjiang.eshop.commodity.dto.PropertyUpdateDTO;
import com.jiangjiang.eshop.commodity.domain.PropertyEntity;
import com.jiangjiang.eshop.commodity.operation.Category;
import com.jiangjiang.eshop.commodity.operation.Properties;
import com.jiangjiang.eshop.commodity.operation.QueryCategoryPropertyOperation;
import com.jiangjiang.eshop.common.bean.SpringApplicationContext;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author Jianghaowen
 * @Date 2021/6/1
 * @Description: 商品属性Service
 * @Version V1.0
 **/
@Service
public class PropertyService extends ServiceImpl<PropertyMapper, PropertyEntity> {

    @Autowired
    private PropertyMapper propertyMapper;
    @Autowired
    private PropertyConvert propertyConvert;
    @Autowired
    private SpringApplicationContext context;

    /**
     * 属性列表分页查询
     *
     * @param dto 分页查询dto
     * @return 查询结果
     */
    public IPage<PropertyEntity> queryForPage(PropertyPageQueryDTO dto) {
        Page<PropertyEntity> page = new Page<>(dto.getPageNo(), dto.getPageSize(),true);
        LambdaQueryWrapper<PropertyEntity> queryWrapper = Wrappers.<PropertyEntity>lambdaQuery().like(
                !Strings.isEmpty(dto.getPropertyName()), PropertyEntity::getPropertyName, dto.getPropertyName());
        return propertyMapper.selectPage(page, queryWrapper);
    }


    /**
     * 新增属性
     *
     * @param dto 新增属性DTO
     * @return 查询结果
     */
    public Boolean saveProperty(PropertySaveDTO dto) {
        return this.save(propertyConvert.convertSaveDto2Entity(dto));
    }

    /**
     * 根据id查询接口
     * @param id 属性id
     * @return 查询结果
     */
    public PropertyDTO queryById(Long id){
        LambdaQueryWrapper<PropertyEntity> eq = Wrappers.<PropertyEntity>lambdaQuery().eq(PropertyEntity::getId, id);
        return propertyConvert.convertEntity2Dto(this.getOne(eq));
    }

    /**
     * 根据id集合查询属性数据
     * @param ids 属性id集合
     * @return 查询结果
     */
    public List<PropertyDTO> queryByIds(List<Long> ids){
        LambdaQueryWrapper<PropertyEntity> eq = Wrappers.<PropertyEntity>lambdaQuery().in(PropertyEntity::getId, ids);
        return propertyConvert.convertEntities2DTOList(this.list(eq));
    }

    /**
     * 更新属性
     * @param dto 属性更新DTO
     * @return 处理结果
     */
    public Boolean updateProperty(PropertyUpdateDTO dto) {
        PropertyDTO propertyDTO = this.queryById(dto.getId());
        if(Objects.isNull(propertyDTO)){
            log.warn("未查询到属性数据");
            return false;
        }
        propertyDTO.setPropertyDesc(dto.getPropertyDesc());
        return this.updateById(propertyConvert.convertDto2Entity(propertyDTO));
    }


    /**
     * 根据类目id查询类目关联的所有属性
     * @param categoryId 类目id
     * @return 查询结果
     */
    public Properties getPropertiesByCategoryId(Long categoryId) throws Exception {
        QueryCategoryPropertyOperation operation = context.getBean(QueryCategoryPropertyOperation.class);
        Category category = new Category(categoryId);
        return category.execute(operation);
    }
}
